In [1]:
import os
import matplotlib.pyplot as plt
from matplotlib import style
style.use("fivethirtyeight")
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
In [2]:
datadir="C:\\Users\\personal\\Desktop\\Covid-19"
In [3]:
os.listdir(datadir)
Out[3]:
['.ipynb_checkpoints',
 'corona virus report',
 'Covid 19 data analysis and visualization.ipynb',
 'Jan to feb corona',
 'Time series data',
 'World population',
 'zip files data']
In [4]:
coronavirusreport="C:\\Users\\personal\\Desktop\\Covid-19\\corona virus report"
In [5]:
os.listdir(coronavirusreport)
Out[5]:
['covid_19_clean_complete.csv']
In [6]:
coviddataset=pd.read_csv("C:\\Users\\personal\\Desktop\\Covid-19\\corona virus report\\covid_19_clean_complete.csv")
In [7]:
coviddataset.head()
Out[7]:
Province/State Country/Region Lat Long Date Confirmed Deaths Recovered
0 NaN Afghanistan 33.0000 65.0000 1/22/20 0 0 0
1 NaN Albania 41.1533 20.1683 1/22/20 0 0 0
2 NaN Algeria 28.0339 1.6596 1/22/20 0 0 0
3 NaN Andorra 42.5063 1.5218 1/22/20 0 0 0
4 NaN Angola -11.2027 17.8739 1/22/20 0 0 0
In [8]:
coviddataset.describe().transpose()
Out[8]:
count mean std min 25% 50% 75% max
Lat 17136.0 22.238772 24.364494 -41.4545 8.599125 23.914900 41.316075 71.7069
Long 17136.0 23.356497 71.494642 -135.0000 -20.026050 20.921188 85.953175 178.0650
Confirmed 17136.0 523.227766 4798.964492 0.0000 0.000000 0.000000 28.000000 140886.0000
Deaths 17136.0 20.676237 267.670675 0.0000 0.000000 0.000000 0.000000 10779.0000
Recovered 17136.0 160.086018 2252.351806 0.0000 0.000000 0.000000 1.000000 62570.0000
In [9]:
coviddataset.isnull().sum()
Out[9]:
Province/State    11832
Country/Region        0
Lat                   0
Long                  0
Date                  0
Confirmed             0
Deaths                0
Recovered             0
dtype: int64
In [10]:
coviddataset["day"]=coviddataset["Date"].str.split('/').str[1].astype(int)
coviddataset["month"]=coviddataset["Date"].str.split('/').str[0].astype(int)
coviddataset["year"]=coviddataset["Date"].str.split('/').str[2].astype(int)
coviddataset.head()
Out[10]:
Province/State Country/Region Lat Long Date Confirmed Deaths Recovered day month year
0 NaN Afghanistan 33.0000 65.0000 1/22/20 0 0 0 22 1 20
1 NaN Albania 41.1533 20.1683 1/22/20 0 0 0 22 1 20
2 NaN Algeria 28.0339 1.6596 1/22/20 0 0 0 22 1 20
3 NaN Andorra 42.5063 1.5218 1/22/20 0 0 0 22 1 20
4 NaN Angola -11.2027 17.8739 1/22/20 0 0 0 22 1 20
In [11]:
plt.figure(figsize=(10,6))
coviddataset.groupby("month").mean()["Confirmed"].plot()
plt.xlabel("month")
plt.ylabel("cases conformed")
plt.title("No of positive cases conformed")
Out[11]:
Text(0.5, 1.0, 'No of positive cases conformed')
In [12]:
plt.figure(figsize=(10,6))
coviddataset.groupby("month").mean()["Deaths"].plot()
plt.xlabel("month")
plt.ylabel("Deaths conformed")
plt.title("No of Deaths")
Out[12]:
Text(0.5, 1.0, 'No of Deaths')
In [13]:
plt.figure(figsize=(10,6))
coviddataset.groupby("month").mean()["Recovered"].plot()
plt.xlabel("month")
plt.ylabel("Recovered cases")
plt.title("Recovered")
Out[13]:
Text(0.5, 1.0, 'Recovered')
In [14]:
plt.figure(figsize=(10,6))
coviddataset.groupby("month")["Confirmed"].plot()
plt.title("no of cases conformed")
Out[14]:
Text(0.5, 1.0, 'no of cases conformed')
In [15]:
plt.figure(figsize=(10,6))
coviddataset.groupby("month")["Deaths"].plot()
plt.title("no of deaths conformed")
Out[15]:
Text(0.5, 1.0, 'no of deaths conformed')
In [16]:
plt.figure(figsize=(10,6))
coviddataset.groupby("month")["Recovered"].plot()
plt.title("no of Recovered")
Out[16]:
Text(0.5, 1.0, 'no of Recovered')
In [17]:
ax=plt.figure(figsize=(19,12.5))
ax.add_subplot(121)
sns.lineplot(x="month",y="Confirmed",data=coviddataset,color="r")
ax.add_subplot(122)
sns.lineplot(x="month",y="Deaths",data=coviddataset,color="black")
Out[17]:
<matplotlib.axes._subplots.AxesSubplot at 0x15eeedb1f98>
In [18]:
fig=plt.figure(figsize=(19,10))
ax=fig.add_subplot(121,projection="3d")
ax.scatter(coviddataset["Confirmed"],coviddataset["Recovered"],coviddataset["Deaths"],color="r")
ax.set(xlabel='\nConfirmed',ylabel='\nRecovered',zlabel='\nDeaths')
Out[18]:
[Text(0.5, 0, '\nDeaths'),
 Text(0.5, 0, '\nRecovered'),
 Text(0.5, 0, '\nConfirmed')]
In [19]:
import plotly.graph_objects as go
In [20]:
x=coviddataset.sort_values("Confirmed",ascending=False)["Confirmed"][0:30]
fig=go.Figure(
                    data=[go.Bar(y=list(x))],
                    layout_title_text="Highest no of cases per day"
                   )
fig.data[0].marker.line.width=2
fig.data[0].marker.line.color="black"
fig.show()
In [21]:
y=coviddataset.sort_values("Deaths",ascending=False)["Deaths"][0:30]
fig=go.Figure(
                    data=[go.Bar(y=list(y))],
                    layout_title_text="Highest no of Deaths per day"
                   )
fig.data[0].marker.line.width=2
fig.data[0].marker.line.color="black"
fig.show()
In [22]:
from plotly.subplots import make_subplots
In [23]:
dataset=coviddataset[["Recovered","Deaths","Confirmed"]]
In [24]:
#make figure with subplots
fig=make_subplots(rows=1,cols=1,specs=[[{"type":"surface"}]])
#adding surface
fig.add_surface(z=dataset)
fig.update_layout(
                   showlegend=False,
                   height=800,
                   width=800,
                   title_text="3D model"
                 )
fig.show()
In [25]:
import plotly.express as px
In [26]:
df=coviddataset
fig=px.scatter_3d(df,x="Confirmed",y="Recovered",z="Deaths")
fig.show()
In [27]:
fig=go.Figure()

fig.add_trace(go.Surface(z=dataset.values.tolist(),colorscale="Viridis"))

fig.update_layout(
                   width=800,
                   height=900,
                   autosize=False,
                   margin=dict(t=0,b=0,l=0,r=0),
                   template="plotly_white",
                 )
fig.update_scenes(
                   aspectratio=dict(x=1,y=1,z=0.7),
                   aspectmode="manual"
                  )

fig.update_layout(
                 updatemenus=[
                               dict(
                                    type="buttons",
                                    direction="left",
                                    buttons=list([
                                                   dict(
                                                       args=["type","surface"],
                                                       label="3D Surface",
                                                       method="restyle"
                                                       ),
                                                   dict(
                                                       args=["type","heatmap"],
                                                       label="heatmap",
                                                       method="restyle"
                                                       )
                                                 ]),
                                                pad={"r":10,"t":10},
                                                showactive=True,
                                                x=0.11,
                                                xanchor="left",
                                                y=1.1,
                                                yanchor="top"
                                             )
                             ]
                 )
fig.update_layout(
    annotations=[
        dict(text="Trace type:", showarrow=False,
                             x=0, y=1.08, yref="paper", align="left")
    ]
)

fig.show()
In [28]:
#px.set_mapbox_access_token(open(".mapbox_token").read())
fig=px.scatter_mapbox(df,lat="Lat",lon="Long",size="Confirmed",color="Deaths",size_max=40,zoom=10,
                     color_continuous_scale=px.colors.cyclical.IceFire)
fig.update_layout(
                  mapbox_style="white-bg",
                  mapbox_layers=[{
                                   "below": 'traces',
                                   "sourcetype": "raster",
                                   "source": [
                                    "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
                                            ]
                                }]
                 )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
In [29]:
plt.figure(figsize=(10,6))
sns.scatterplot(x="Confirmed",y="Long",data=coviddataset,color="red")
Out[29]:
<matplotlib.axes._subplots.AxesSubplot at 0x15ef59e4908>
In [30]:
plt.figure(figsize=(10,6))
sns.scatterplot(x="Confirmed",y="Lat",data=coviddataset,color="green")
Out[30]:
<matplotlib.axes._subplots.AxesSubplot at 0x15ef405fa58>
In [31]:
plt.figure(figsize=(10,6))
sns.scatterplot(x="Long",y="Lat",hue="Confirmed",data=coviddataset)
Out[31]:
<matplotlib.axes._subplots.AxesSubplot at 0x15ef58765f8>
In [32]:
import folium
import webbrowser
from folium.plugins import HeatMap
In [33]:
latitude=30.5
longitude=114.3
dup=coviddataset.copy()
dup["count"]=1
dup.head()
p="orgin"
def worldmap(location=[latitude,longitude],zoom=9):
    map=folium.Map(location=location,control_state=True,zoom_start=zoom)
    return map
fmap=worldmap()
folium.TileLayer("cartodbpositron").add_to(fmap)
sg=folium.FeatureGroup(name="target").add_to(fmap)
folium.Marker([latitude,longitude],
              popup=p,
              icon=folium.Icon(color="red")).add_to(sg)
HeatMap(data=dup[["Lat","Long","count"]].groupby(["Lat","Long"]).sum().reset_index().values.tolist(),
       radius=8,max_zoom=13,name='Heat Map').add_to(fmap)
folium.LayerControl(collapsed=False).add_to(fmap)
fmap
Out[33]: